package CS4310.csueb.visual;

///////////////////////////////////////////////////////////////////////////
//class MapTileContainer 
//
//  Wraps storage structure for Active or Cached Map Tiles
//
//  The game map will use game tiles which allow 
//
//    1.  Game Space expansion at future dates or during development
//    2.  Localized qualities such as harm, protection, no-access, etc.
//    3.  Localized placement of items that can be found or dropped
//    4.  Movement based on transitions from tile to tile.
//    5.  Scrollable Map Characteristics similar to Google maps
//    6.  Vertical Levels abstraction
//    7.  Simplified methods to define collisions and other interactions
//    8.  Special characteristics of localized game space not yet visioned
//
//  @author Brian L. Donat
//  Created 2011-11-08
//  Updated 2011-11-10 by Bikas to flesh out the code in empty container
//  Updated 2011-11-12 by Brian to add final bounds and store the tile and adjustments
//  Updated 2011-11-27 by Brian to allow maps of various sizes
//                               Use 2nd constructor by Bikas
//  Updated 2011-11-28 by Brian to allow saving identifying info for 
//                               game restores
//
///////////////////////////////////////////////////////////////////////////

public class MapTileContainer {

  // private storage structure to store map tiles
  // Origin at 1,1
	
	// MTCWIDTH and MTCHEIGHT MUST BE EVEN INTEGER NUMBERS!
	private int MTCWidth = 12;  // Default WIDTH for this Map.
	private int MTCHeight = 10;  // Default HEIGHT for this Map.
	
	//Use a fixed array for now, but we can change to arrayList or other storage later
	private MapTile tileStore[][]; 
	
	//Identifying info needed to restore a saved game
	private int xOffset, yOffset;
	private String mapName;

	
//********* default constructor **********//  
  public MapTileContainer()
  {
	  //assuming 800*500 Map and 100*100 MapTiles
	  tileStore = new MapTile[MTCWidth][MTCHeight];
	  xOffset = 0;
	  yOffset = 0;
  }
  
//********* map size constructor **********//
  public MapTileContainer(int width, int height) {
	  
	  // Note that the width and height must be appropriate for the map size or errors.
	  MTCWidth = width;
	  MTCHeight = height;
	  tileStore = new MapTile[MTCWidth][MTCHeight];
	  xOffset = 0;
	  yOffset = 0;
  }

//********* store MapTile **********//  
  public boolean storeMapTile(MapTile mt, int xOffset, int yOffset) 
  {

	  if (!existsMapTile(xOffset, yOffset)) {
		  
	   //   Tiles can be negative so adjust the array indexes.
		 xOffset += (MTCWidth/2);
		 yOffset += (MTCHeight/2);
		 tileStore[xOffset][yOffset] = mt;
		 return true;
	  }
	  return false;
  }
  
//********* retrieve MapTile **********//  
  public MapTile retrieveMapTile(int xOffset, int yOffset) 
  {
    //   Tiles can be negative so adjust the array indexes.
	  xOffset += (MTCWidth/2);
	  yOffset += (MTCHeight/2);
	  
    // Returns null, if the map tile has not been set.
	//   This must be evaluated outside the container. 
	return tileStore[xOffset][yOffset];	//could be null.
  }
  
//********* retrieve MapTile **********//  
  public boolean existsMapTile(int xOffset, int yOffset) 
  {
	  int x1, y1;
	  
	//   Force a derefence of xOffset and yOffset so that math isn't performed twice in this class.
	  x1 = (xOffset + 0); y1 = (yOffset + 0);
	  
	//   Tiles can be negative so adjust the array indexes.
	  x1 = (int) x1 + (int)(MTCWidth/2);
	  y1 = (int) y1 + (int)(MTCHeight/2);
 	  
	// Test for existence of a MapTile.
	  if (tileStore[x1][y1] == null) {
		  return false;
	  }
	  else return true;
  }

  
//********* set xOffset **********//  
  public void setXOffset(int xOff) 
  {
	xOffset = xOff;
  }

  
//********* set yOffset **********//  
  public void setYOffset(int yOff) 
  {
	yOffset = yOff;
  }
  

//********* get xOffset **********//  
  public int getXOffset() 
  {
	return xOffset;
  }

  
//********* get yOffset **********//  
  public int getYOffset() 
  {
	return yOffset;
  }
  
  
//********* set mapName **********//  
  public void setMapName(String map) 
  {
	mapName = map;
  }
  
  
//********* get mapName **********//  
  public String getMapName() 
  {
	return mapName;
  }
  
  
//********* get Width **********//  
  public int getWidth() 
  {
	return MTCWidth;
  }
  
  
//********* get Height **********//  
  public int getHeight() 
  {
	return MTCHeight;
  }
}